﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TableViewHeader.cs" company="DissingNellemann">
//   
// All rights reserved </copyright>
// <summary>
//   The table view header.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Website.Models.Styling
{
    using System.Collections.Generic;

    /// <summary>
    ///     The table view header.
    /// </summary>
    public class TableViewHeader
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="TableViewHeader" /> class.
        /// </summary>
        public TableViewHeader()
        {
            this.Items = new List<TableViewHeaderItem>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the action.
        /// </summary>
        public string Action { get; set; }

        /// <summary>
        ///     Gets or sets the items.
        /// </summary>
        public List<TableViewHeaderItem> Items { get; set; }

        /// <summary>
        ///     Gets or sets the skip.
        /// </summary>
        public int? Skip { get; set; }

        /// <summary>
        ///     Gets or sets the take.
        /// </summary>
        public int? Take { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get table header.
        /// </summary>
        /// <param name="Action">
        /// The action.
        /// </param>
        /// <param name="Take">
        /// The take.
        /// </param>
        /// <param name="CurrentOrderBy">
        /// The current order by.
        /// </param>
        /// <param name="Items">
        /// The items.
        /// </param>
        /// <param name="SortAscending">
        /// The sort ascending.
        /// </param>
        /// <returns>
        /// The <see cref="TableViewHeader"/>.
        /// </returns>
        public static TableViewHeader GetTableHeader(
            string Action, int Take, string CurrentOrderBy, List<TableViewHeaderItem> Items, bool SortAscending)
        {
            var tableHeader = new TableViewHeader();
            foreach (TableViewHeaderItem item in Items)
            {
                if (!string.IsNullOrWhiteSpace(CurrentOrderBy))
                {
                    if (item.IsSortable && CurrentOrderBy.Equals(item.SortableName))
                    {
                        if (SortAscending)
                        {
                            item.IsCurrentSortedByAsc = true;
                            item.IsCurrentSortedByDesc = false;
                        }
                        else
                        {
                            item.IsCurrentSortedByAsc = false;
                            item.IsCurrentSortedByDesc = true;
                        }
                    }
                }

                item.Take = Take;
                item.Action = Action;

                tableHeader.Items.Add(item);
            }

            return tableHeader;
        }

        #endregion
    }
}